<template>
    <div class='approval'>
        <el-card class="box-card" style="margin-top: 110px;">
            <div class='title'>
                {{$t('approvalDispatch.title1')}}
            </div>
            <div class='menu-search'>
                    <!-- 业务ID -->
                    <div class='menu-title'>
                        <div class='searchItem'>
                        <span>{{$t('approvalDispatch.orderId')}}</span>
                        <el-input v-model='listQuery.orderId' :placeholder="$t('rule.orderId')" size='small'></el-input>
                    </div>
                    <!-- 交易流水号 -->
                    <div class='searchItem'>
                        <span>{{$t('paymentHistory.batch')}}</span>
                        <el-input v-model='listQuery.btId' :placeholder="$t('rule.batch')" size='small'></el-input>
                    </div>
                    <!-- 标识 -->
                    <div class='searchItem'>
                        <span>{{$t('paymentHistory.flag')}}</span>
                        <el-select v-model='listQuery.flag' size='small'>
                            <el-option
                            v-for='item in batchArr'
                            :key='item.value'
                            :label='item.label'
                            :value='item.value'>
                            </el-option>
                        </el-select>
                    </div>
                    <!-- 创建时间 -->
                    <div class='searchItem searchItem-date'>
                        <span>{{$t('approvalDetail.smsCrtTime')}}</span>
                        <el-date-picker
                        size='small'
                        v-model="listQuery.beginTime"
                        type="date"
                        value-format="yyyy-MM-dd"
                        :placeholder="$t('rule.beginDate')">
                        </el-date-picker>
                        <b>{{$t('approval.go')}}</b>
                        <el-date-picker
                        size='small'
                          v-model="listQuery.endTime"
                          type="date"
                          value-format="yyyy-MM-dd"
                          :placeholder="$t('rule.endDate')">
                        </el-date-picker>
                    </div>
                </div>
                <div class='menu-title-btn'>
                  <button type='button' @click='search'>
                      {{$t('btn.query')}}
                  </button>
                  <button type='button' @click='reset'>
                      {{$t('btn.reset')}}
                  </button>
                  <button type='button' @click="handleDownload" :disabled="expDis"  v-if="paymentHistory_btn_export">
                    {{$t('btn.export')}}
                </button>
              </div>
            </div>
        </el-card>
        <el-card class='box-card' style="margin-top: 10px;margin-bottom: 20px;">
            <div class='title'>
                {{$t('approvalDispatch.title2')}}
            </div>
            <el-table
                v-loading="listLoading"
                ref='multipleTable'
                :data='tableData'
                tooltip-effect='dark'
                style='width: 100%'
                border
                stripe
                size='mini'>
                <!-- 业务ID -->
                <el-table-column
                prop='orderId' align="center" header-align="center" 
                :label="$t('approvalDispatch.orderId')"
                width='140'>
                </el-table-column>
                <!-- 交易号 -->
                <el-table-column
                prop='btId' align="center" header-align="center" 
                :label="$t('paymentHistory.trade')"
                width='240'>
                </el-table-column>
                <!-- 流水号 -->
                <el-table-column
                prop='tranId' align="center" header-align="center" 
                :label="$t('paymentHistory.flow')"
                width='140'>
                </el-table-column>
                <!-- 金额 -->
                <el-table-column
                prop='price' align="center" header-align="center" 
                :label="$t('approval.applyAmt')"
                width='140'>
                </el-table-column>
                <!-- 标识 -->
                <el-table-column
                prop='flag' align="center" header-align="center" 
                :label="$t('paymentHistory.flag')"
                width='100'>
                <template slot-scope="scope">
                  <span v-if="scope.row.flag=== 'manualAccounting'">{{$t('paymentHistory.manualAccounting')}}</span>
                  <span v-if="scope.row.flag=== 'normal'">{{$t('paymentHistory.bank')}}</span>
                  <span v-if="scope.row.flag=== 'cashout'">{{$t('paymentHistory.cashout')}}</span>
                  <span v-if="scope.row.flag=== 'rollover'">{{$t('paymentHistory.rollover')}}</span>
                  <span v-if="scope.row.flag=== 'partial'">{{$t('processing.result4')}}</span>
                </template>
                </el-table-column>
                <!-- 还款方式 -->
                <el-table-column
                prop='payType' align="center" header-align="center" 
                :label="$t('paymentHistory.repayMethod')"
                width='140'>
                </el-table-column>
                <!-- 还款码 -->
                <el-table-column
                prop='paymentCode' align="center" header-align="center" 
                :label="$t('paymentHistory.repayCode')"
                width='180'>
                </el-table-column>
                <!-- 创建时间 -->
                <el-table-column
                prop='createTime' align="center" header-align="center" 
                :label="$t('grouping.crtTime')"
                 width='160'
                show-overflow-tooltip>
                </el-table-column>
            </el-table>
            <div class="pagination-container">
              <el-pagination
              @size-change="handleSizeChange"
              @current-change="handleCurrentChange"
              :current-page="listQuery.page"
              :page-sizes="[5, 10, 20, 30]"
              :page-size="listQuery.limit"
              layout="total, sizes, prev, pager, next, jumper"
              :total="total">
              </el-pagination>
            </div>
        </el-card>
    </div>
</template>

<script>
import { mapGetters } from "vuex";
import { page, pageExport } from 'api/trust/paymentHistory/index'
export default {
  data() {
    return {
      paymentHistory_btn_export: false,
      expDis: true,
      listLoading: false,
      total: null,
      listQuery: {
        limit: 5,
        page: 1,
        orderId: "",//业务ID
        btId: "",
        beginTime: "",
        endTime: "",
        flag: ""
      },
      tableData: [],
      batchArr: [
        {
          value: 'manualAccounting',
          label: this.$t('paymentHistory.manualAccounting')
        },
        {
          value: 'normal',
          label: this.$t('paymentHistory.bank')
        },
        {
          value: 'cashout',
          label: this.$t('paymentHistory.cashout')
        },
        {
          value: 'rollover',
          label: this.$t('paymentHistory.rollover')
        },
        {
          value: 'partial',
          label: this.$t('processing.result4')
        }
      ]
    };
  },
  computed: {
    ...mapGetters(["elements", "paymentHisPage"])
  },
  created() {
    if (this.paymentHisPage.has(this.$route.path)) {
      this.listQuery = this.paymentHisPage.get(this.$route.path)
    }
    this.getList();
    this.paymentHistory_btn_export = this.elements['paymentHistory:btn_export'];
  },
  methods: {
    // 点击导出
    handleDownload() {
      this.listLoading = true;
      pageExport(this.listQuery)
      .then(response => {
        response.data.forEach(item => {
          // 标识
          if (item.flag === 'manualAccounting') {
            item.status = this.$t('paymentHistory.manualAccounting')
          } else if(item.flag === 'normal') {
            item.status = item.status = this.$t('paymentHistory.bank')
          } else if(item.flag === 'cashout') {
            item.status = item.status = this.$t('paymentHistory.cashout')
          } else if(item.flag === 'rollover') {
            item.status = item.status = this.$t('paymentHistory.rollover')
          } else if(item.flag === 'partial') {
            item.status = item.status = this.$t('processing.result4')
          } 
        })
        require.ensure([], () => {
          const { export_json_to_excel } = require('@/vendor/Export2Excel')
          const tHeader = [ this.$t('approvalDispatch.orderId'), this.$t('paymentHistory.trade'), this.$t('paymentHistory.flow'), this.$t('approval.applyAmt'), this.$t('paymentHistory.flag'), this.$t('paymentHistory.repayMethod'), this.$t('paymentHistory.repayCode'), this.$t('grouping.crtTime')];
          const filterVal = [ 'orderId', 'btId', 'tranId', 'price', 'flag', 'payType', 'paymentCode', 'createTime'];
          const list = response.data;
          const data = this.formatJson(filterVal, list)
          export_json_to_excel(tHeader, data, this.$t('route.paymentHistory'))
          this.listLoading = false;
        })
      })
    },
    formatJson(filterVal, jsonData) {
      return jsonData.map(v => filterVal.map(j => v[j]))
    },
    getList() {
      this.listLoading = true;
      this.$store.dispatch('savePaymentHisPage', { path: this.$route.path, listQuery: this.listQuery });
      page(this.listQuery)
        .then(response => {
          if (response.status === 200) {
            response.data.rows.forEach(item => {
              let re = /(?=(?!\b)(\d{3})+$)/g;
              let str = String(item.price)
              item.price =  str.replace(re, ',');
            })
            this.expDis = false;
            this.listLoading = false;
            this.tableData = response.data.rows;
            this.total = Number(response.data.total);
          }
        })
    },
    //查询
    search() {
      this.listQuery.page = 1;
      this.getList();
    },
    //重置
    reset() {
      this.listQuery.limit = 5;
      this.listQuery.page = 1;
      this.listQuery.orderId = "";
      this.listQuery.btId = "";
      this.listQuery.beginTime = "";
      this.listQuery.endTime = "";
      this.listQuery.flag = "";
    },
    handleSizeChange(val) {
      this.listQuery.limit = val;
      this.getList();
    },
    handleCurrentChange(val) {
      this.listQuery.page = val;
      this.getList();
    }
  }
};
</script>

<style scoped lang='scss'>
.box-card {
  width: 95%;
  margin-left: 2.5%;
  margin-right: 2.5%;
}
.title {
  line-height: 30px;
  font-size: 16px;
  display: flex;
  align-items: center;
  font-weight: bold;
}
.title:before{
  display: block;
  content: "";
  width: 3px;
  height: 16px;
  background-color: #309afe;
}
.title:before {
  margin-right: 8px;
}
::before {
  box-sizing: inherit;
  -webkit-tap-highlight-color: transparent;
}
.menu-title {
  width: 100%;
  box-sizing: border-box;
  background: #ffffff;
  display: flex;
  align-items: center;
  flex-wrap: wrap;
  .searchItem {
    display: flex;
    align-items: center;
    padding: 10px  0;
    span {
      display: inline-block;
      width: 100px;
      text-align: right;
      font-size: 12px;
      color: #5f5f61;
      padding-right: 5px;
    }
    div:nth-child(2) {
      width: 150px;
    }
  }
  .searchItem-date {
    display: flex;
    align-items: center;
    padding: 10px  0;
    b {
      margin: 0 10px;
      font-weight: normal;
      font-size: 12px;
      color: #5f5f61;
    }
    div {
       width: 150px;
    }
  }
}
.menu-title-btn {
  padding: 10px 0;
  box-sizing: border-box;
  button {
    padding: 0 20px;
    height: 28px;
    background: #20a0ff;
    color: #fff;
    outline: none;
    border-width: 0;
    border-radius: 4px;
    font-size: 12px;
    margin-left: 10px;
    &:nth-child(1) {
      margin-left: 0;
    }
    &:nth-child(2) {
      background: #ebf5ff;
      color: #20a0ff;
      border: 1px solid #abd5ff;
    }
  }
}
.menu-list {
  padding: 20px 0;
  box-sizing: border-box;
  button {
    padding: 0 20px;
    height: 28px;
    background: #20a0ff;
    color: #fff;
    outline: none;
    border-width: 0;
    border-radius: 4px;
    font-size: 12px;
    margin-left: 10px;
    &:nth-child(1) {
      margin-left: 0;
    }
  }
  button[disabled] {
    padding: 0 20px;
    height: 28px;
    background: #c1e0ff;
    color: #fafbff;
    outline: none;
    border-width: 0;
    border-radius: 4px;
    font-size: 12px;
  }
}
.handler {
  width: 90%;
  display: flex;
  justify-content: space-between;
  align-items: center;
    span {
      display: inline-block;
      width: 80px;
      text-align: center;
      font-size: 14px;
      font-weight: bold;
    }
    div:nth-child(2) {
      flex: 1;
    }
}
</style>